﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Globalization;

namespace remes.BLoc
{
   public class TranslationText : INotifyPropertyChanged
   {
      public TranslationText(TranslationEntry entry, string language)
      {
         Entry = entry;
         Language = language;
         Text = String.Empty;
         if (entry.IsTextEmpty || entry.IsTextNumber || !entry.IsReadable || !entry.IsModifiable)
         {
            State = TranslationTextState.NoTranslationNeeded;
         }
         else
         {
            State = TranslationTextState.New;
         }
         TranslatorsComment = String.Empty;
      }

      public TranslationText(TranslationEntry entry, XElement xText)
      {
         Entry = entry;
         Language = (string)xText.Attribute("Language");
         Text = (string)xText.Attribute("Text");
         State = (TranslationTextState)Enum.Parse(typeof(TranslationTextState), (string)xText.Attribute("State"));
         TranslatorsComment = (string)xText.Attribute("TranslatorsComment");
      }

      public TranslationEntry Entry { get; private set; }

      public string Language { get; private set; }

      private string _Text;

      public string Text
      {
         get { return _Text; }
         set
         {
            _Text = value;
            if (BLocSettings.Instance.AutoSetStateTranslated)
            {
               State = TranslationTextState.Translated;
            }
            NotifyPropertyChnaged("Text");
         }
      }

      private TranslationTextState _State;

      public TranslationTextState State
      {
         get { return _State; }
         set
         {
            _State = value;
            NotifyPropertyChnaged("State");
         }
      }

      private string _TranslatorsComment;

      public string TranslatorsComment
      {
         get { return _TranslatorsComment; }
         set
         {
            _TranslatorsComment = value;
            NotifyPropertyChnaged("TranslatorsComment");
         }
      }

      internal void Save(XElement xText)
      {
         xText.Add(
            new XAttribute("Language", Language ?? String.Empty),
            new XAttribute("Text", Text ?? String.Empty),
            new XAttribute("State", State.ToString()),
            new XAttribute("TranslatorsComment", TranslatorsComment ?? String.Empty));
      }

      #region INotifyPropertyChanged Members

      protected void NotifyPropertyChnaged(string propertyName)
      {
         var temp = PropertyChanged;
         if (temp != null)
         {
            temp(this, new PropertyChangedEventArgs(propertyName));
         }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      #endregion

      internal bool IsRedundant()
      {
         if (!String.IsNullOrWhiteSpace(TranslatorsComment))
            return false;

         if (!String.IsNullOrWhiteSpace(Text))
            return false;

         if (Entry.IsTextEmpty || Entry.IsTextNumber || !Entry.IsReadable || !Entry.IsModifiable)
         {
            if (State != TranslationTextState.NoTranslationNeeded)
               return false;
         }
         else
         {
            if (State != TranslationTextState.New)
               return false;
         }

         if (String.Compare(Language, Entry.OriginalLanguage, StringComparison.OrdinalIgnoreCase) == 0
            && String.Compare(Entry.OriginalText, Text, false, CultureInfo.GetCultureInfo(Language)) != 0)
            return false;

         return true;
      }
   }

   public enum TranslationTextState
   {
      New,
      OriginalTextChanged,
      AutoTranslated,
      Translated,
      NoTranslationNeeded
   }
}
